package com.dwxt.cdms.util;

import com.dwxt.cdms.util.UtilDao.Point;

public class ElectronicFenceUtil {

    //是否需要报警
    public static boolean IsInPolygon(Point p, double[][] ptPolygon, int ncount){
        int rcross = 0;
        int lcross = 0;

        for (int i = 0; i < ncount; i++){
            double[] p1 = ptPolygon[i];
            double[] p2 = ptPolygon[(i + 1) % ncount]; //相邻两条边p1,p2
            if (p1[1] == p2[1]){
                continue;
            }
            if (p.getY() < min(p1[1], p2[1])){
                continue;
            }
            if (p.getY() >= max(p1[1], p2[1])){
                continue;
            }
            double x = (p.getY() - p1[1])*(p2[0] - p1[0]) / (p2[1] - p1[1]) + p1[0];
            if (x > p.getX()){
                rcross++; //只统计单边交点
            }
            if (x < p.getX()){
                lcross++;
            }
        }
        //在区域内
        if (rcross % 2 ==1 && lcross % 2==1){
            return false;
        }
        //在区域外
        return true;
    }


    /**
     * @return 最小值
     */
    public static double min(double double1, double double2)  {
       if (double1<double2){
           return double1;
       }else {
           return double2;
       }
    }

    /**
     * @return 最大值
     */
    public static double max(double double1, double double2)  {
        if (double1<double2){
            return double2;
        }else {
            return double1;
        }
    }

}
